<template>
  <div>
    <el-form ref="form" :rules="rules" :model="form" label-width="100px">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="form.name" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="form.code" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="form.manager" placeholder="请选择活动区域">
          <el-option
            v-for="item in employList"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="form.introduce" type="textarea" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="deptSubmit">{{
          isEdit ? "修改" : "确定"
        }}</el-button>
        <el-button @click="cancle">取消</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {
  addDeptApi,
  editDeptApi,
  getSimpleInfoApi,
  reGetDeptListApi
} from '@/api/departments'
export default {
  name: 'DeptDialog',
  props: {
    deptsId: {
      type: [Number, String],
      required: true
    },
    isEdit: {
      type: Boolean,
      required: true
    },
    validlist: {
      type: Array,
      required: true
    }
  },
  data() {
    // 验证编码重复
    const validcode = (rule, value, callback) => {
      // 这里需要进行判断,如果是编辑状态,那么就先排除出自己那一项,然后参与判断,如果是添加状态,那么就无需处理,直接判断
      // 为何可以直接修改父传子数据?因为这也是只读的,并没有违反单项数据流规则
      const codeList = this.isEdit
        ? this.validlist
          .filter((item) => item.id !== this.deptsId)
          .map((item) => item.code)
        : this.validlist.map((item) => item.code)
      // 首先生成code列表
      // 查询code列表是否包含当前输入的value,如果包含那就拦截并显示已存在,否则放行
      codeList.includes(value)
        ? callback(new Error(`部门编码${value}已存在`))
        : callback()
    }
    // 验证部门名重复
    const validName = (rule, value, callback) => {
      // 筛选pid等于当前的部门id的元素

      // 如果是编辑状态的话

      // 因为编辑部门传入的值是自身,所以需要找到自己的兄弟级,通过自己的pid来查找
      // 首先需要获取自己

      // 此时用nPid获取所有的兄弟

      const nameRes = this.isEdit
        ? this.validlist
          .filter(
            (item) =>
              item.pid ===
                  this.validlist.find((obj) => obj.id === this.deptsId).pid &&
                item.id !== this.deptsId
          )
          .map((item) => item.name)
        : this.validlist
          .filter((item) => item.pid === this.deptsId)
          .map((item) => item.name)
      nameRes.includes(value)
        ? callback(new Error(`部门名称${value}已存在`))
        : callback()
    }
    return {
      form: {
        code: '',
        introduce: '',
        manager: '',
        name: ''
      },
      employList: [],
      // 验证规则
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '部门名称要求1-50个字符',
            trigger: 'blur'
          },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 50,
            message: '部门编码要求1-50个字符',
            trigger: 'blur'
          },
          { validator: validcode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          {
            min: 1,
            max: 300,
            message: '部门介绍要求1-300个字符',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  async created() {
    // 根据id获取部门详情
    if (this.isEdit) {
      const res = await reGetDeptListApi(this.deptsId)
      this.form = res.data
      console.log(this.form)
    }
    // 获取人员详情
    const res = await getSimpleInfoApi()
    this.employList = res.data
  },
  methods: {
    // 点击确定/修改按钮
    deptSubmit() {
      // 弹框兜底验证
      this.$refs.form.validate(async(valid) => {
        // 验证不通过直接停止
        if (!valid) return
        // 执行判定,分别调用不同接口,获取不同信息,实现不同功能
        const res = this.isEdit
          ? await editDeptApi(this.form)
          : await addDeptApi({ pid: this.deptsId, ...this.form })
        // 发送成功以后
        // 子向父传值
        this.$emit('addevent', res.message)
      })
    },
    cancle() {
      this.$emit('gooutevent')
    }
  }
}
</script>

<style lang="scss" scoped>
.el-form {
  padding: 0 60px 0 20px;
}
.el-select {
  width: 100%;
}
</style>
